<template>
  <el-form :inline="true">
    <el-form-item>
      <el-select v-model="queryParam.cateId" clearable
                 placeholder="收入&支出" @change="handleQuery">
        <el-option label="收入" value="1"/>
        <el-option label="支出" value="-1"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-select v-model="queryParam.typeId" clearable
                 placeholder="选择类型" @change="handleQuery">
        <el-option v-for="type in Dic.walletLogType()"
                   :key="type.value"
                   :label="type.text"
                   :value="type.value"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-date-picker v-model="queryParam.dateArray"
                      :shortcuts="Dic.DateShortcuts"
                      @change="handleQuery"
                      type="daterange"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="YYYY-MM-DD"/>
    </el-form-item>
    <!--        <el-form-item>-->
    <!--          <el-input v-model="queryParam.keyword"-->
    <!--                    @keyup.enter="handleQuery"-->
    <!--                    placeholder="输入关键字"-->
    <!--                    type="text"-->
    <!--                    auto-complete="off"/>-->
    <!--        </el-form-item>-->
    <el-form-item>
      <el-button type="warning" :loading="loadingExport" @click="listExport">导出</el-button>
    </el-form-item>
    <el-form-item>
      <el-text v-text="`合计：${formatRMB(this.statistics.allTotal)}`" tag="b"/>
      <el-text v-text="`(${this.statistics.allNumber}笔)`" type="info"/>
    </el-form-item>
    <el-form-item>
      <el-text v-text="`收入：${formatRMB(this.statistics.inTotal)}`" type="success" tag="b"/>
      <el-text v-text="`(${this.statistics.inNumber}笔)`" type="info"/>
    </el-form-item>
    <el-form-item>
      <el-text v-text="`支出：${formatRMB(this.statistics.outTotal)}`" type="danger" tag="b"/>
      <el-text v-text="`(${this.statistics.outNumber}笔)`" type="info"/>
    </el-form-item>
    <el-form-item>
      <el-text v-text="`无变化：${formatRMB(this.statistics.zeroTotal)}`" type="info"/>
      <el-text v-text="`(${this.statistics.zeroNumber}笔)`" type="info"/>
    </el-form-item>
  </el-form>
  <el-table :data="tableData.list" v-loading="loading" border>
    <el-table-column label="时间" prop="createTime" width="200" align="center"/>
    <el-table-column label="变动类型" prop="typeName" width="160" align="center"/>
    <el-table-column label="资金类型" prop="walletName" width="160" align="center"/>
    <el-table-column label="变动金额" prop="changeAmount" width="160" align="right">
      <template v-slot="{row:{changeAmount}}">
        <span v-if="changeAmount > 0" class="success" v-text="`+` + changeAmount"/>
        <span v-else-if="changeAmount < 0" class="danger" v-text="changeAmount"/>
        <span v-else v-text="changeAmount"/>
      </template>
    </el-table-column>
    <el-table-column label="账户余额" prop="balanceAmount" width="160" align="right"/>
    <el-table-column label="销售人" prop="saleName" width="120" align="center"/>
    <el-table-column label="备注" prop="remark" align="center"/>
    <el-table-column label="操作人" prop="operatorName" width="120" align="center"/>
  </el-table>
  <el-pagination layout="->,total,sizes,prev,pager,next,jumper"
                 v-model:current-page="queryParam.page"
                 v-model:page-size="queryParam.limit"
                 :total="tableData.total" background
                 @current-change="loadList"
                 @size-change="loadList"/>
</template>

<script lang="ts">
import walletApi from "@/api/wallet";
import base from "@/utils/base";
import {ElMessage} from "element-plus";

export default {
  name: "orderList",
  mixins: [base],
  props: {
    walletId: {},
    typeList: {},
  },
  data() {
    return {
      queryParam: {
        page: 1,
        limit: 10,
        typeId: "",
        walletId: "",
        keyword: "",
        dateArray: []
      },
      tableData: {
        list: [],
        total: 0,
      },
      statistics: {
        allTotal: 0,
        allNumber: 0,
        inTotal: 0,
        inNumber: 0,
        outTotal: 0,
        outNumber: 0,
        zeroTotal: 0,
        zeroNumber: 0
      },
      loading: false,
      loadingExport: false
    };
  },
  created() {
    this.loadList();
    this.loadStatistics()
  },
  watch: {
    walletId(value) {
      this.queryParam.walletId = value;
      this.handleQuery();
    },
  },
  methods: {
    handleQuery() {
      this.queryParam.page = 1;
      this.loadList();
      this.loadStatistics()
    },
    loadList() {
      this.loading = true;
      walletApi.logList(this.queryParam).then(res => {
        this.loading = false;
        this.tableData = res.data
      })
    },
    loadStatistics() {
      walletApi.logStatistics(this.queryParam).then(res => {
        this.statistics = res.data
      })
    },
    listExport() {
      let [start, end] = this.queryParam.dateArray;
      if (!start || !end) {
        return ElMessage.info("请先选择时间")
      }
      this.loadingExport = true;
      walletApi.logListExport(this.queryParam).then(res => {
        this.loadingExport = false;
        return this.exportHandle(res, `${start}-${end}资金变动记录导出.xlsx`)
      })
    },
  }
}
</script>
<style lang="scss" scoped>

</style>